package ext

import android.animation.AnimatorInflater
import android.graphics.drawable.Drawable
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import app.AppContext
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.github.ybq.android.spinkit.SpinKitView
import com.hontech.kdxclient.R
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

fun ImageView.setAnim() {
    this.setOnTouchListener { _, event ->

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                val animator = AnimatorInflater.loadAnimator(this.context, R.animator.animator_to_max)
                animator.setTarget(this)
                animator.start()
            }

            MotionEvent.ACTION_UP -> {
                val animator = AnimatorInflater.loadAnimator(this.context, R.animator.animator_to_min)
                animator.setTarget(this)
                animator.start()
            }
        }

        false
    }
}

fun ImageView.setImageWithUrl(url: String, progress: SpinKitView, radius: Int) {
    this.setImageResource(R.drawable.shape_translate)
    progress.visibility = View.VISIBLE
    this.setTag(R.id.imageId, url)
    val options = RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).skipMemoryCache(true)
        .error(R.drawable.ic_image_load_error).transform(RoundedCornersTransformation(radius, 0))
    Glide.with(AppContext).load(url).apply(options).listener(object : RequestListener<Drawable> {

        override fun onLoadFailed(
            e: GlideException?,
            model: Any?,
            target: Target<Drawable>?,
            isFirstResource: Boolean
        ): Boolean {
            val info = this@setImageWithUrl.getTag(R.id.imageId)
            if (info != null && info == url) {
                progress.visibility = View.GONE
            }
            return false
        }

        override fun onResourceReady(
            resource: Drawable?,
            model: Any?,
            target: Target<Drawable>?,
            dataSource: DataSource?,
            isFirstResource: Boolean
        ): Boolean {
            val info = this@setImageWithUrl.getTag(R.id.imageId)
            if (info != null && info == url) {
                progress.visibility = View.GONE
            }
            return false
        }

    }).into(this)
}

fun ImageView.setImageWithUrl(url: String) {
    val options = RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).skipMemoryCache(true)
        .error(R.drawable.ic_image_load_error)
    Glide.with(AppContext).load(url).apply(options).into(this)
}

fun ImageView.setImageWithUrl(url: String, progress: SpinKitView) {
    this.setImageResource(R.drawable.shape_translate)
    progress.visibility = View.VISIBLE
    this.setTag(R.id.imageId, url)
    val options = RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).skipMemoryCache(true)
        .error(R.drawable.ic_image_load_error)
    Glide.with(AppContext).load(url).apply(options).listener(object : RequestListener<Drawable> {

        override fun onLoadFailed(
            e: GlideException?,
            model: Any?,
            target: Target<Drawable>?,
            isFirstResource: Boolean
        ): Boolean {
            val info = this@setImageWithUrl.getTag(R.id.imageId)
            if (info != null && info == url) {
                progress.visibility = View.GONE
            }
            return false
        }

        override fun onResourceReady(
            resource: Drawable?,
            model: Any?,
            target: Target<Drawable>?,
            dataSource: DataSource?,
            isFirstResource: Boolean
        ): Boolean {
            val info = this@setImageWithUrl.getTag(R.id.imageId)
            if (info != null && info == url) {
                progress.visibility = View.GONE
            }
            return false
        }

    }).into(this)
}
